package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.MemberProfileDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;

import com.itheima.service.MemberProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author Ning
 * @date 2020/8/21 14:38
 */

@Service(interfaceClass =MemberProfileService.class )
@Transactional
public class MemberProfileServiceImpl implements MemberProfileService {

   @Autowired
   private MemberProfileDao memberProfileDao;

   @Autowired
    MemberDao memberDao;

    /**
     * 添加
     * @param member
     */
    @Override
    public void add(Member member) {

        member.setRegTime(new Date());

        Date time=new Date();
        int year=time.getYear();
        int year1 = member.getBirthday().getYear();
        int age=year-year1+1;
        member.setAge(age);
        memberProfileDao.add(member);

    }


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //根据条件分页查询检查项信息
        Page<Member> page = memberProfileDao.selectByCondition(queryPageBean.getQueryString());
        //构建分页数据返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        // 判断是否被引用
        int count = memberProfileDao.selectMemberProfileDaoReferencedCount(id);
        if (count > 0 ){
            throw new RuntimeException("当前检查项被引用,无法删除!");
        }
        // 可以删除
        memberProfileDao.deleteById(id);
    }

    @Override
    public Member findById(int id) {
        return memberProfileDao.findById(id);
    }
    /**
     * 更新检查项数据
     * @param member
     */
    @Override
    public void edit(Member member) {
        memberProfileDao.edit(member);
    }



}

